using Microsoft.Phone.Controls.Maps.AutomationPeers;
using Microsoft.Phone.Controls.Maps.Core;
using Microsoft.Phone.Controls.Maps.Design;
using Microsoft.Phone.Controls.Maps.Overlays;
using System;
using System.ComponentModel;
using System.Globalization;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Threading;
namespace Microsoft.Phone.Controls.Maps
{
	public sealed class Map : MapCore
	{
		private static class LogEntry
		{
			public const string StartSession = "0";
			public const string ChangeCredentials = "2";
		}
		private static readonly string version = Map.GetVersion();
		private WeakEventListener<Map, object, PropertyChangedEventArgs> _weakMapCredentials;
		private LoadingErrorMessage loadingErrorMessage;
		private string logServiceUriFormat;
		private Guid sessionId = Guid.Empty;
		private DispatcherTimer timer;
		private DispatcherTimer networkChangePollTimer;
		public MapForeground MapForeground
		{
			get;
			private set;
		}
		[TypeConverter(typeof(MapModeConverter))]
		public override MapMode Mode
		{
			get
			{
				return base.Mode;
			}
			set
			{
				base.Mode = value;
			}
		}
		[TypeConverter(typeof(ApplicationIdCredentialsProviderConverter))]
		public new CredentialsProvider CredentialsProvider
		{
			get
			{
				return base.CredentialsProvider;
			}
			set
			{
				base.CredentialsProvider = value;
			}
		}
		public Map()
		{
			base.LoadingError += new EventHandler<LoadingErrorEventArgs>(this.Map_LoadingError);
			base.CredentialsProvider = new ApplicationIdCredentialsProvider();
			this.Mode = new RoadMode();
			base.ScaleVisibility = 1;
			base.ZoomBarVisibility = 1;
			this.MapForeground = new MapForeground(this);
			base.RootLayer.get_Children().Add(this.MapForeground);
			RoutedEventHandler routedEventHandler = delegate(object sender, RoutedEventArgs e)
			{
				if (this.timer != null)
				{
					this.timer.remove_Tick(new EventHandler(this.LogStartSession));
				}
				if (this.networkChangePollTimer != null)
				{
					this.networkChangePollTimer.remove_Tick(new EventHandler(this.NetworkChangePollTimer_Tick));
				}
			};
			base.add_Unloaded(routedEventHandler);
		}
		~Map()
		{
			if (this._weakMapCredentials != null)
			{
				this._weakMapCredentials.Detach();
				this._weakMapCredentials = null;
			}
		}
		protected override void OnCultureChanged(DependencyPropertyChangedEventArgs eventArgs)
		{
			base.OnCultureChanged(eventArgs);
			if (this.MapForeground.Culture == null || eventArgs.get_OldValue() as string == this.MapForeground.Culture)
			{
				this.MapForeground.Culture = (eventArgs.get_NewValue() as string);
			}
		}
		protected override void OnCredentialsProviderChanged(DependencyPropertyChangedEventArgs eventArgs)
		{
			base.OnCredentialsProviderChanged(eventArgs);
			INotifyPropertyChanged notifyPropertyChanged = eventArgs.get_OldValue() as INotifyPropertyChanged;
			if (notifyPropertyChanged != null && this._weakMapCredentials != null)
			{
				this._weakMapCredentials.Detach();
				this._weakMapCredentials = null;
			}
			INotifyPropertyChanged newCredentials = eventArgs.get_NewValue() as INotifyPropertyChanged;
			if (newCredentials != null)
			{
				this._weakMapCredentials = new WeakEventListener<Map, object, PropertyChangedEventArgs>(this);
				this._weakMapCredentials.OnEventAction = delegate(Map instance, object source, PropertyChangedEventArgs leventArgs)
				{
					instance.Credentials_PropertyChanged(source, leventArgs);
				};
				this._weakMapCredentials.OnDetachAction = delegate(WeakEventListener<Map, object, PropertyChangedEventArgs> weakEventListener)
				{
					newCredentials.remove_PropertyChanged(new PropertyChangedEventHandler(weakEventListener.OnEvent));
				};
				newCredentials.add_PropertyChanged(new PropertyChangedEventHandler(this._weakMapCredentials.OnEvent));
			}
			this.Log("2");
		}
		protected override void OnFirstFrame()
		{
			base.OnFirstFrame();
			MapConfiguration.GetSection("v1", "WP7SLMapControl", null, new MapConfigurationCallback(this.ConfigurationLoadedAfterFirstFrame));
			if (!DesignerProperties.get_IsInDesignTool() && !NetworkInterface.GetIsNetworkAvailable())
			{
				base.ThrowLoadingException(new ConfigurationNotLoadedException());
				DispatcherTimer dispatcherTimer = new DispatcherTimer();
				dispatcherTimer.set_Interval(TimeSpan.FromSeconds(5.0));
				this.networkChangePollTimer = dispatcherTimer;
				this.networkChangePollTimer.add_Tick(new EventHandler(this.NetworkChangePollTimer_Tick));
				this.networkChangePollTimer.Start();
			}
		}
		private static string GetVersion()
		{
			string result = string.Empty;
			string[] array = Assembly.GetExecutingAssembly().get_FullName().Split(new char[]
			{
				','
			});
			if (array.Length > 1)
			{
				result = array[1].Replace("Version=", string.Empty).Trim();
			}
			return result;
		}
		private void ConfigurationLoadedAfterFirstFrame(MapConfigurationSection config, object userState)
		{
			if (config != null)
			{
				this.logServiceUriFormat = config["LogServiceUriFormat"];
				if (!string.IsNullOrEmpty(this.logServiceUriFormat))
				{
					this.logServiceUriFormat = this.logServiceUriFormat.Replace("{UriScheme}", "HTTP");
					this.timer = new DispatcherTimer();
					this.timer.set_Interval(new TimeSpan(0, 0, 0, 5));
					this.timer.add_Tick(new EventHandler(this.LogStartSession));
					this.timer.Start();
				}
			}
		}
		private void LogStartSession(object sender, EventArgs e)
		{
			this.timer.Stop();
			this.timer.remove_Tick(new EventHandler(this.LogStartSession));
			this.timer = null;
			this.sessionId = Guid.NewGuid();
			this.Log("0");
		}
		private void Log(string entry)
		{
			if (this.CredentialsProvider != null)
			{
				this.CredentialsProvider.GetCredentials(delegate(Credentials credentials)
				{
					this.Log(entry, credentials);
				});
			}
		}
		private void Log(string entry, Credentials credentials)
		{
			try
			{
				if (!string.IsNullOrEmpty(this.logServiceUriFormat) && !DesignerProperties.GetIsInDesignMode(this) && this.sessionId != Guid.Empty && NetworkInterface.GetIsNetworkAvailable())
				{
					string text = string.Format(CultureInfo.get_InvariantCulture(), this.logServiceUriFormat, new object[]
					{
						entry,
						credentials,
						Map.version,
						this.sessionId,
						base.Culture
					});
					HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(text);
					httpWebRequest.BeginGetResponse(new AsyncCallback(this.LogResponse), httpWebRequest);
				}
			}
			catch (WebException)
			{
				this.OnCredentialsError();
			}
			catch (NotSupportedException)
			{
			}
		}
		private void LogResponse(IAsyncResult result)
		{
			bool flag = false;
			try
			{
				HttpWebRequest httpWebRequest = (HttpWebRequest)result.get_AsyncState();
				HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(result);
				flag = (httpWebResponse.get_StatusCode() != 401);
			}
			catch (WebException ex)
			{
				HttpWebResponse httpWebResponse2 = ex.get_Response() as HttpWebResponse;
				flag = (httpWebResponse2 == null || httpWebResponse2.get_StatusCode() != 401);
			}
			if (flag)
			{
				base.get_Dispatcher().BeginInvoke(delegate
				{
					this.OnCredentialsValid();
				});
				return;
			}
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.OnCredentialsError();
			});
		}
		private void NetworkChangePollTimer_Tick(object sender, EventArgs e)
		{
			if (NetworkInterface.GetIsNetworkAvailable())
			{
				this.networkChangePollTimer.Stop();
				this.networkChangePollTimer.remove_Tick(new EventHandler(this.NetworkChangePollTimer_Tick));
				this.networkChangePollTimer = null;
				ConfigurationNotLoadedException ex = base.LoadingException as ConfigurationNotLoadedException;
				if (ex != null)
				{
					base.get_Dispatcher().BeginInvoke(delegate
					{
						base.LoadingException = null;
						if (this.loadingErrorMessage != null)
						{
							base.RootLayer.get_Children().Remove(this.loadingErrorMessage);
							this.loadingErrorMessage = null;
						}
					});
				}
			}
		}
		private void OnCredentialsError()
		{
			base.ThrowLoadingException(new CredentialsInvalidException());
		}
		private void OnCredentialsValid()
		{
			CredentialsInvalidException ex = base.LoadingException as CredentialsInvalidException;
			if (ex != null)
			{
				base.LoadingException = null;
				if (this.loadingErrorMessage != null)
				{
					base.RootLayer.get_Children().Remove(this.loadingErrorMessage);
					this.loadingErrorMessage = null;
				}
			}
		}
		private void Credentials_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			this.Log("2");
		}
		private void Map_LoadingError(object sender, LoadingErrorEventArgs e)
		{
			if (this.loadingErrorMessage == null)
			{
				this.loadingErrorMessage = new LoadingErrorMessage();
				base.RootLayer.get_Children().Add(this.loadingErrorMessage);
			}
			if (e.LoadingException is UriSchemeNotSupportedException)
			{
				this.loadingErrorMessage.SetUriSchemeError(base.Culture);
				return;
			}
			if (e.LoadingException is ConfigurationNotLoadedException)
			{
				this.loadingErrorMessage.SetConfigurationError(base.Culture);
				return;
			}
			if (e.LoadingException is CredentialsInvalidException)
			{
				this.loadingErrorMessage.SetCredentialsError(base.Culture);
			}
		}
		protected override AutomationPeer OnCreateAutomationPeer()
		{
			return new MapAutomationPeer(this);
		}
	}
}
